﻿<%@ WebHandler Language="C#" Class="register" %>

using System;
using System.Web;
using OneShoppingBLL;
using OneShoppingModel;
using OneshoppingWeb;
using OneShoppingUtility;
using System.Web.SessionState;

public class register : IHttpHandler, IRequiresSessionState
{
    private int mark = -1;
    bool result = false;
    public void ProcessRequest(HttpContext context)
    {
       
     
        try
        {
            string referrer = Convert.ToString(context.Request.UrlReferrer);//获取那个页面访问该接口
           if (referrer.IndexOf("Backstage/login.html")!= -1)
           {
               mark = 2;
            }
           else if (referrer.IndexOf("Front/index.html") != -1)
            {
                mark = 3;
            }
            else {
                context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.ILLEGAL_ACCESS));//访问路径非法
                return;
            }
        }
        catch {
            context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.ILLEGAL_ACCESS) );//访问路径非法
            return;
        }
      
        if(mark==-1){
            context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.ILLEGAL_ACCESS));//访问路径非法
            return;
        }
        string LoginName = context.Request["LoginName"].ToString();
        string LoginPwd = context.Request["LoginPwd"].ToString();
        string againPassWord = context.Request["againPassWord"].ToString();
        string phone = context.Request["phone"].ToString();
        if (LoginPwd != againPassWord)
        {
            context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.PASSWORD_DIF));//两次密码不一样
            return;
        }

        SystemUser user = SystemUserBLL.GetInstance().GetEntity(r => r.LoginName == LoginName);
        if (user != null)
        {
            context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.USER_FIND));//用户存在
            return;
        }
        else
        {
            SystemUser sysUser = new SystemUser();
            sysUser.LoginName = LoginName;
            sysUser.LoginPwd = LoginPwd;
            sysUser.UserPhone = phone;
            sysUser.RoleId = mark;
            sysUser.Status = 1;
            try
            {
                Role role = RoleBLL.GetInstance().GetEntity(r => r.RoleId == sysUser.RoleId);
                if (role == null)
                {
                    context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.ROLE_N_FIND));//角色不存在
                    return;
                }
                if (role.IsUsing == 0)
                {
                    context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.ROLE_N_USE));//角色没有使用
                    return;
                }

                bool results = SystemUserBLL.GetInstance().Insert(sysUser);
                if (results)
                {
                    //{//管理员 
                    //    CompanyUser cUser = new CompanyUser();
                    //    cUser.UserId = SystemUserBLL.GetInstance().GetEntity(r => r.LoginName == LoginName).UserId;
                    //    bool result1 = CompanyUserBLL.GetInstance().Insert(cUser);
                    //    if (result1)
                    //    {
                    //        context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.REGISTER_SUCCESS));//注册成功
                    //    }
                    //    else
                    //    {
                    //        context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.REGISTER_FAIL));//注册失败
                    //        SystemUserBLL.GetInstance().Delete(cUser.UserId);
                    //        return;
                    //    }
                    //}

                    if (mark == 2)
                    {//商家
                        BusinessUser bUser = new BusinessUser();
                        bUser.UserId = SystemUserBLL.GetInstance().GetEntity(r => r.LoginName == LoginName).UserId;
                        result = BusinessUserBLL.GetInstance().Insert(bUser);
                    }
                    if (mark == 3)
                    { //普通用户
                        GeneralUser gUser = new GeneralUser();
                        gUser.UserId = SystemUserBLL.GetInstance().GetEntity(r => r.LoginName == LoginName).UserId;
                        result = GeneralUserBLL.GetInstance().Insert(gUser);

                    }

                    if (result)
                    {
                        context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.REGISTER_SUCCESS));//注册成功
                    }
                    else
                    {
                        SystemUserBLL.GetInstance().Delete(sysUser.UserId);
                        context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.REGISTER_FAIL));//注册失败
                        return;
                    }
                }
                else
                {
                    context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.REGISTER_FAIL));//注册失败
                }
            }
            catch
            {
                context.Response.Write(UtilityJson.ResultToJson((int)Protocol_user.REGISTER_FAIL));//注册失败
            }
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}